package com.c5p1ng.distributed.lock.factory;

import com.c5p1ng.distributed.lock.config.LockConfig;
import com.c5p1ng.distributed.lock.core.ILock;
import com.c5p1ng.distributed.lock.core.ReentrantLock;
import com.c5p1ng.distributed.lock.enums.LockType;
import org.redisson.api.RedissonClient;

import static com.c5p1ng.distributed.lock.enums.LockType.Reentrant;

/**
 * 分布式锁工厂类
 */
public class LockFactory {
    private RedissonClient redissonClient;
    private LockConfig lockConfig;

    public LockFactory(LockConfig lockConfig, RedissonClient redissonClient) {
        this.redissonClient = redissonClient;
        this.lockConfig = lockConfig;
        System.out.println("初始化分布式锁获取工厂");
    }

    public ILock getLock() {
        return getLock(Reentrant);
    }

    public ILock getLock(LockType lockType) {
        switch(lockType) {
            case Reentrant:
                return new ReentrantLock(redissonClient, lockConfig);
            default:
                return new ReentrantLock(redissonClient, lockConfig);
        }
    }
}
